package cn.wolfcode.wolf2w.service.impl;


import cn.wolfcode.wolf2w.domain.Banner;
import cn.wolfcode.wolf2w.query.BannerQuery;
import cn.wolfcode.wolf2w.repository.BannerRepository;
import cn.wolfcode.wolf2w.service.IBannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class BannerServiceImpl implements IBannerService {


    //在这里引用后期后台项目引用会导致报错
   /* @Value("${sms.url}")
    private String smsUrl;
    @Value("${sms.Key")
    private String appKey;*/


    @Autowired
    private BannerRepository repository;
    @Autowired
    private MongoTemplate template;

    @Override
    public void save(Banner banner) {
        repository.save(banner);
    }

    @Override
    public void update(Banner banner) {
        Banner banner1 = repository.findById(banner.getId()).get();
        if (StringUtils.hasLength(banner.getCoverUrl())){
        banner1.setCoverUrl(banner.getCoverUrl());}
        if (StringUtils.hasLength(banner.getRefId())){
        banner1.setRefId(banner.getRefId());}
        if (StringUtils.hasLength(banner.getTitle())){
            banner1.setTitle(banner.getTitle());
        }
        if (StringUtils.hasLength(banner.getSubTitle())){
            banner.setSubTitle(banner.getSubTitle());
        }
        banner1.setState(banner.getState());
        banner1.setSequence(banner.getSequence());
        repository.save(banner1);
    }


    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public Banner get(String id) {
        Banner banner = repository.findById(id).get();
        return banner;
    }

    @Override
    public List<Banner> list() {
        List<Banner> banners = repository.findAll();
        return banners;
    }

    //分页查询
    @Override
    public Page<Banner> query(BannerQuery qo) {
        Query query=new Query();
        //repository只能针对所有数据进行分页
        //template不能直接对数据进行分页
        //使用template间接分页
        //pageResult 核心字段
        //totalCount 总记录数
        long totalcount = template.count(query, Banner.class);
        if (totalcount==0){
            //空的分页对象
            return Page.empty();
        }
        //list 当前页显示数据集合
        Pageable pageable= PageRequest.of(qo.getCurrentPage()-1,qo.getPageSize(), Sort.Direction.ASC,"_id");
        query.with(pageable);
        List<Banner> list = template.find(query, Banner.class);
        return new PageImpl<>(list,pageable,totalcount);
    }

    @Override
    public void saveOrUpdate(Banner banner) {
        if(StringUtils.hasLength(banner.getId())){
            this.update(banner);
        }else {
            this.save(banner);
        }
    }

    @Override
    public List<Banner> queryTravleBanner(int type) {
        Pageable pageable = PageRequest.of(0, 5, Sort.Direction.ASC, "sequence");
        List<Banner> list = repository.findByStateAndType(Banner.STATE_NORMAL, type);
        return list;
    }

    @Override
    public List<Banner> queryStrategyBanner(int typeStrategy) {
        Pageable pageable = PageRequest.of(0, 5, Sort.Direction.ASC, "sequence");
        List<Banner> list = repository.findByStateAndType(Banner.STATE_NORMAL, typeStrategy);
        return list;
    }

}
